package com.apps.ftapp.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.apps.ftapp.R;
import com.apps.ftapp.activity.BookDetailActivity;
import com.apps.ftapp.activity.GestureFilpSucActivity;
import com.apps.ftapp.activity.LessonDetailActivity;
import com.apps.ftapp.activity.LianXiActivity;
import com.apps.ftapp.activity.MyStActivity;
import com.apps.ftapp.activity.SubjectActivity;
import com.apps.ftapp.activity.TimeTableActivity;
import com.apps.ftapp.activity.ZhenTiNewActivity;
import com.apps.ftapp.bean.BookBean;
import com.apps.ftapp.bean.CategoriesBean;
import com.apps.ftapp.custom.NoScrollGridView;
import com.apps.ftapp.holder.IndexPlanHolder;
import com.apps.ftapp.holder.IndexTitleHolder;
import com.apps.ftapp.model.BaseItem;
import com.apps.ftapp.model.BookModel;
import com.apps.ftapp.model.IndexModel1;
import com.apps.ftapp.model.IndexTitle;
import com.apps.ftapp.model.LessonModel;
import com.apps.ftapp.model.SubjectModel;
import com.apps.ftapp.model.TableModel;
import com.apps.ftapp.model.ZhenTi;
import com.apps.ftapp.ui.ExamActivity;
import com.apps.ftapp.util.ActivitySkipUtil;
import com.apps.ftapp.util.Async;
import com.apps.ftapp.util.Constant;
import com.apps.ftapp.util.L;
import com.apps.ftapp.util.SharedPreferencesUtil;
import com.apps.ftapp.util.Tools;
import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cz.msebera.android.httpclient.Header;
import es.dmoral.toasty.Toasty;

/**
 * Created by lyli on 2017/10/2.
 */

public class IndexAdapter extends BaseAdapter implements View.OnClickListener {

    private Context context;
    private List<BaseItem> dataList;
    private LayoutInflater layoutInflater;
    private ImageLoader imageLoader = ImageLoader.getInstance();
    private DisplayImageOptions options;
    private String tempdata = "";

    private List<BookBean> bookList;


    private IndexModel1 indexModel1;

    private MyClickListener mListener;

    private List<CategoriesBean> fenleiList;

    private List<ZhenTi.TestpapersBean> zhentiList;
    private final Gson gson = new Gson();

    private String ttname;


    //    private int[] imgs ={R.drawable.jiaoyuxue,R.drawable.jiaoyuxinlixue,R.drawable.xinlixue,R.drawable.gonggongjichuzhishi,R.drawable.jiaoyufalvfagui,R.drawable.jiaoyuzhiyedaode,R.drawable.gongjichangshi,R.drawable.shizhengredian};
//    private String[] titles = {"教育学","教育心理学","心理学","公共基础知识","教育法律法规","教育知识道德","公基常识","时政热点"};
//    private String[] ids = {"1","2","3","4","5","6","7","8"};
//    private String[] timgs = {"http://iweishi.cn/files/tmp/2017/09-14/1553277d0011281694.jpg","http://iweishi.cn/files/tmp/2017/09-14/15535971955c462034.jpg"
//    ,"http://iweishi.cn/files/tmp/2017/09-14/1554477e17ad209855.jpg","http://iweishi.cn/files/tmp/2017/09-14/155538a560c1306076.jpg","http://iweishi.cn/files/tmp/2017/09-14/1556033b80ee387863.jpg"
//    ,"http://iweishi.cn/files/tmp/2017/09-14/1556320691ce624003.jpg","http://iweishi.cn/files/tmp/2017/09-14/1557117f129d729871.jpg","http://iweishi.cn/files/tmp/2017/09-14/155746ad4990497346.jpg"};
    private String[] subTitle = {"", "", "更多", "更多", "更多", "更多", "更多", "更多"};

    //教师资格证
    private String[] jiaoshi = {"", ""};


    //自定义接口，用于回调按钮点击事件到Activity
    public interface MyClickListener {
        void clickListener(View v);
    }


    public IndexAdapter(Context context, List<BaseItem> dataList,
                        IndexModel1 indexModel1,
                        List<CategoriesBean> fenleiList,
                        List<BookBean> bookList,
                        MyClickListener listener,
                        String ttname) {
        this.context = context;
        this.dataList = dataList;
        this.indexModel1 = indexModel1;
        this.fenleiList = fenleiList;
        this.bookList = bookList;
        this.ttname = ttname;
//        this.ttname = SharedPreferencesUtil.getData(context,"yijiName","")+"";
//        if("".equals(this.ttname)){
//            this.ttname = "教师招聘";
//        }

        layoutInflater = LayoutInflater.from(this.context);

        mListener = listener;
        //显示图片的配置  
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.ic_launcher)
                .showImageOnFail(R.mipmap.ic_launcher).cacheInMemory(true).cacheOnDisk(true).bitmapConfig(Bitmap.Config.RGB_565).build();
        imageLoader.init(ImageLoaderConfiguration.createDefault(context));


    }


    public void addRes(IndexModel1 indexModel1) {
        this.indexModel1 = indexModel1;
        notifyDataSetChanged();
    }


    //响应按钮点击事件,调用子定义接口，并传入View
    @Override
    public void onClick(View v) {
        mListener.clickListener(v);
    }


    @Override
    public int getCount() {
        if (null != dataList && dataList.size() > 0)
            return dataList.size();
        else
            return 0;
    }

    @Override
    public Object getItem(int i) {
        return dataList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }


    //不同的item布局类型的数量
    @Override
    public int getViewTypeCount() {
        return 7;
    }

    //不同布局的item的类型
    @Override
    public int getItemViewType(int position) {
        return dataList.get(position).getItem_type();
    }


    /**
     * @param i
     * @param view
     * @param viewGroup
     * @return
     */
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        //Log.i("main",getItemViewType(i)+"====");

        switch (getItemViewType(i)) {
            case BaseItem.TYPE_TITLE:
                //题库练习
                IndexTitleHolder indexTitleHolder;
                final IndexTitle tit = (IndexTitle) dataList.get(i);
                if (view == null) {
                    view = LayoutInflater.from(context).inflate(R.layout.fragment_index_listview_title, null);
                    view.setTag(tit.getItem_type());
                    indexTitleHolder = new IndexTitleHolder(view);
                    view.setTag(indexTitleHolder);
                } else {
                    indexTitleHolder = (IndexTitleHolder) view.getTag();
                }
                indexTitleHolder.title.setText(tit.getTitle());
                indexTitleHolder.subTitle.setText(tit.getSubTitle());
                //  Log.i("MAIN",Tools.isNull(tit.getSubTitle()));
                //隐藏副标题
                if ("".equals(Tools.isNull(tit.getSubTitle()))) {
                    indexTitleHolder.more.setVisibility(View.GONE);
                } else {
                    indexTitleHolder.more.setVisibility(View.VISIBLE);
                }
                //隐藏标题栏
                if ("".equals(Tools.isNull(tit.getTitle()))) {
                    indexTitleHolder.tabLinear.setVisibility(View.GONE);
                } else {
                    indexTitleHolder.tabLinear.setVisibility(View.VISIBLE);
                }

                if ("题库练习".equals(Tools.isNull(tit.getTitle()))) {
                    indexTitleHolder.paddView.setVisibility(View.GONE);
                    //按照
//                    indexTitleHolder.tabLinear.setVisibility(View.GONE);
//                    indexTitleHolder.more.setVisibility(View.GONE);

                } else {
//                    indexTitleHolder.paddView.setVisibility(View.VISIBLE);
                }


                if ("教师资格证".equals(Tools.isNull(tit.getTitle()))) {
                    indexTitleHolder.title.setText(ttname);
//                    indexTitleHolder.paddView.setVisibility(View.GONE);
                }


                if ("推荐图书".equals(Tools.isNull(tit.getTitle()))) {
                    indexTitleHolder.paddView.setVisibility(View.GONE);
                }


                indexTitleHolder.tabLinear.setTag(((IndexTitle) dataList.get(i)).getTitle());
                indexTitleHolder.tabLinear.setOnClickListener(this);
                break;

            case BaseItem.TYPE_TAB:
                //GridView
                //   Log.i("main",getItemViewType(i)+"===11111111111=");
                GridViewHolder gridViewHolder;
                if (view == null) {
                    // gridView
                    //  Log.i("main",getItemViewType(i)+"===11111111111=");
                    view = LayoutInflater.from(context).inflate(R.layout.fragment_index_gridview, null);
                    gridViewHolder = new GridViewHolder(view);
                    view.setTag(gridViewHolder);
                } else {
                    gridViewHolder = (GridViewHolder) view.getTag();
                }
                final IndexGridAdapter indexGridAdapter = new IndexGridAdapter(context, fenleiList);
                gridViewHolder.gridView.setAdapter(indexGridAdapter);


                gridViewHolder.gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                        HashMap<String, Object> map = new HashMap();
                        map.put("id", fenleiList.get(i).getId());
                        map.put("title", fenleiList.get(i).getName());
                        ActivitySkipUtil.skipAnotherForActivity(context, LianXiActivity.class, map);
                    }
                }); //仿照52修改之前的代码


//                NewFenleiHolder holder;
//                if (view == null) {
//                    view = layoutInflater.from(context).inflate(R.layout.item_newhome, null);
//                    holder = new NewFenleiHolder(view);
//                    view.setTag(holder);
//                } else {
//                    holder = (NewFenleiHolder) view.getTag();
//                }
//                holder.lin1.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View view) {
//                        Intent intent = new Intent();
//                        intent.setClass(context, SubjectActivity.class);
//                        context.startActivity(intent);
//                    }
//                });
//
//                holder.lin2.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View view) {
//
//                    }
//                });
//                holder.lin3.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View view) {
//                        Intent intent = new Intent();
//                        intent.setClass(context, ZhenTiNewActivity.class);
//                        intent.putExtra("type", 1);
//                        context.startActivity(intent);
//                    }
//                });
//                holder.lin4.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View view) {
//                        Intent intent = new Intent();
//                        intent.setClass(context, ZhenTiNewActivity.class);
//                        intent.putExtra("type", 2);
//                        context.startActivity(intent);
//                    }
//                });
//

                break;

            case BaseItem.TYPE_PLAN:
                //学习计划
                IndexPlanHolder indexPlanHolder;
                if (view == null) {
                    // gridView
                    view = LayoutInflater.from(context).inflate(R.layout.fragment_index_listview_plan, null);
                    indexPlanHolder = new IndexPlanHolder(view);
                    view.setTag(indexPlanHolder);
                } else {
                    indexPlanHolder = (IndexPlanHolder) view.getTag();
                }
                indexPlanHolder.main_rel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (tempdata.equals("") || tempdata == null) {
                            gettimetable();
                        } else {
                            Intent intent = new Intent();
                            intent.setClass(context, TimeTableActivity.class);
                            intent.putExtra("data", tempdata);
                            intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                            context.startActivity(intent);
                        }

                    }
                });
                indexPlanHolder.btn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (tempdata.equals("") || tempdata == null) {
                            gettimetable();
                        } else {
                            Intent intent = new Intent();
                            intent.setClass(context, TimeTableActivity.class);
                            intent.putExtra("data", tempdata);
                            intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                            context.startActivity(intent);
                        }
                    }
                });

                break;

            case BaseItem.TYPE_ZIGEZHENG:


                if ("教师资格证".equals(Tools.isNull(ttname))) {

                    //教师资格证
                    GridBookViewHolder gridBookViewHolder;
                    if (view == null) {
                        // gridView
                        view = LayoutInflater.from(context).inflate(R.layout.fragment_index_listview_zigezheng_gridview, null);
                        gridBookViewHolder = new GridBookViewHolder(view);
                        view.setTag(gridBookViewHolder);
                    } else {
                        gridBookViewHolder = (GridBookViewHolder) view.getTag();
                    }
                    final IndexGridBookAdapter mainGrdiAdapter = new IndexGridBookAdapter(context, indexModel1.getZigezheng());
                    gridBookViewHolder.gridView.setAdapter(mainGrdiAdapter);
                    gridBookViewHolder.gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                            HashMap<String, Object> map = new HashMap();
                            map.put("id", indexModel1.getZigezheng().get(i).getId());
                            map.put("title", indexModel1.getZigezheng().get(i).getTitle());
                            ActivitySkipUtil.skipAnotherForActivity(context, LessonDetailActivity.class, map);
                        }
                    });


                } else {

//                    L.i("main","..教师招聘........");
                    //教师招聘
                    GridBookViewHolder gridBookViewHolder1;
                    if (view == null) {
                        // gridView
                        view = LayoutInflater.from(context).inflate(R.layout.fragment_index_listview_zigezheng_gridview, null);
                        gridBookViewHolder1 = new GridBookViewHolder(view);
                        view.setTag(gridBookViewHolder1);
                    } else {
                        gridBookViewHolder1 = (GridBookViewHolder) view.getTag();
                    }
                    final IndexGridBook2Adapter mainGrdiAdapter1 = new IndexGridBook2Adapter(context, indexModel1.getZhaopin());
                    gridBookViewHolder1.gridView.setAdapter(mainGrdiAdapter1);
                    gridBookViewHolder1.gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                            HashMap<String, Object> map = new HashMap();
                            map.put("id", indexModel1.getZhaopin().get(i).getId());
                            map.put("title", indexModel1.getZhaopin().get(i).getTitle());
                            ActivitySkipUtil.skipAnotherForActivity(context, LessonDetailActivity.class, map);

                        }
                    });

                }


                break;

//            case BaseItem.TYPE_ZHAOPIN:
//                //教师招聘
//                GridBookViewHolder gridBookViewHolder1;
//                if (view == null) {
//                    // gridView
//                    view = LayoutInflater.from(context).inflate(R.layout.fragment_index_listview_zigezheng_gridview, null);
//                    gridBookViewHolder1 = new GridBookViewHolder(view);
//                    view.setTag(gridBookViewHolder1);
//                }else{
//                    gridBookViewHolder1 = (GridBookViewHolder) view.getTag();
//                }
//                final IndexGridBook2Adapter mainGrdiAdapter1 = new IndexGridBook2Adapter(context,indexModel1.getZhaopin());
//                gridBookViewHolder1.gridView.setAdapter(mainGrdiAdapter1);
//                gridBookViewHolder1.gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//                    @Override
//                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//
//                        HashMap<String,Object> map = new HashMap();
//                        map.put("id",indexModel1.getZhaopin().get(i).getId());
//                        map.put("title",indexModel1.getZhaopin().get(i).getTitle());
//                        ActivitySkipUtil.skipAnotherForActivity(context, LessonDetailActivity.class,map);
//
//                    }
//                });
//
//                break;

            case BaseItem.TYPE_BOOK:
                //图书
                BookScrollViewHoldewr gridBookViewHolder2;
                if (view == null) {
                    // gridView
                    view = LayoutInflater.from(context).inflate(R.layout.fragment_index_book_scroll, null);
                    gridBookViewHolder2 = new BookScrollViewHoldewr(view);
                    view.setTag(gridBookViewHolder2);
                } else {
                    gridBookViewHolder2 = (BookScrollViewHoldewr) view.getTag();
                }

                if (null != bookList && bookList.size() > 0) {
                    for (final BookBean d : bookList) {
                        View view1 = LayoutInflater.from(context).inflate(R.layout.fragment_index_scroll_item, null);
                        ImageView imageView = view1.findViewById(R.id.book_img);
                        TextView bookTitle = view1.findViewById(R.id.book_title);
                        TextView bookShl = view1.findViewById(R.id.book_shl);
                        imageLoader.displayImage(d.getLargePicture(), imageView, options);
                        bookTitle.setText(d.getTitle());
                        bookShl.setText(d.getSubtitle());
                        view1.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                HashMap map = new HashMap();
                                map.put("id", d.getId());
                                ActivitySkipUtil.skipAnotherForActivity(context, BookDetailActivity.class, map);

                            }
                        });
                        gridBookViewHolder2.linearLayout.addView(view1);
                    }

                }

                break;
            case BaseItem.TYPE_ZHENTI:
                //真题
                final IndexTitle bean = (IndexTitle) dataList.get(i);
                ZhenTiViewHolder zhenTiViewHolder;
                if (view == null) {
                    // gridView
                    view = LayoutInflater.from(context).inflate(R.layout.fragment_index_listview_zhenti_item, null);
                    zhenTiViewHolder = new ZhenTiViewHolder(view);
                    view.setTag(zhenTiViewHolder);
                } else {
                    zhenTiViewHolder = (ZhenTiViewHolder) view.getTag();
                }

                zhenTiViewHolder.zhentiTitle.setText(bean.getTitle());
                if ("1".equals(Tools.isNull(bean.getAlready()))) {
                    zhenTiViewHolder.zhentiBtn.setText("已做过");
                } else {
                    zhenTiViewHolder.zhentiBtn.setText("未做过");
                }
                zhenTiViewHolder.relzhenti.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
//                        Toasty.normal(context,"222222").show();;
                        HashMap map = new HashMap();
                        map.put("id", bean.getSubTitle());
//                        ActivitySkipUtil.skipAnotherForActivity(context, MyStActivity.class,map);

                        ActivitySkipUtil.skipAnotherForActivity(context, ExamActivity.class, map);
                    }
                });

                break;


        }

        return view;
    }

    /**
     * 分类
     */
    class GridViewHolder {
        public NoScrollGridView gridView;

        public GridViewHolder(View view) {
            gridView = view.findViewById(R.id.gridView);
        }
    }


    /**
     * 教师资格证
     */
    class GridBookViewHolder {
        public NoScrollGridView gridView;

        public GridBookViewHolder(View view) {
            gridView = view.findViewById(R.id.bookGridView);
        }

    }

    //真题
    class ZhenTiViewHolder {

        ImageView zhentiImg;
        TextView zhentiTitle;
        TextView zhentiBtn;
        RelativeLayout relzhenti;

        public ZhenTiViewHolder(View view) {
            relzhenti = view.findViewById(R.id.relzhenti);
            zhentiImg = view.findViewById(R.id.zhenti_img);
            zhentiTitle = view.findViewById(R.id.zhenti_title);
            zhentiBtn = view.findViewById(R.id.zhenti_btn);
        }

    }


    //真题
    class BookScrollViewHoldewr {
        HorizontalScrollView scrollView;
        LinearLayout linearLayout;

        public BookScrollViewHoldewr(View view) {
            scrollView = view.findViewById(R.id.scrollView);
            linearLayout = view.findViewById(R.id.mylinear);
        }

    }

    class NewFenleiHolder {
        LinearLayout lin1;
        LinearLayout lin2;
        LinearLayout lin3;
        LinearLayout lin4;

        public NewFenleiHolder(View view) {

            lin1 = view.findViewById(R.id.lin_model);
            lin2 = view.findViewById(R.id.lin_weeknew);
            lin3 = view.findViewById(R.id.lin_year);
            lin4 = view.findViewById(R.id.lin_match);
        }
    }

    //真题
    class BookViewHoldewr {
        ImageView bookImg;
        TextView bookTitle;
        TextView bookShl;

        public BookViewHoldewr(View view) {
            bookImg = view.findViewById(R.id.book_img);
            bookTitle = view.findViewById(R.id.book_title);
            bookShl = view.findViewById(R.id.book_shl);
        }

    }

    //
//    //真题
//    class BookViewHoldewr{
//        ImageView bookImg;
//        TextView bookTitle;
//        TextView bookShl;
//        public BookViewHoldewr(View view) {
//            bookImg = (ImageView) view.findViewById(R.id.book_img);
//            bookTitle = (TextView) view.findViewById(R.id.book_title);
//            bookShl = (TextView) view.findViewById(R.id.book_shl);
//        }
//
//    }
    private void gettimetable() {
        RequestParams params = new RequestParams();
        params.add("userId", Constant.uid);
        params.add("token", Constant.token);
        Async.post("my/daycron", params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                if (statusCode == 200) {
                    try {
                        Gson gson = new Gson();
                        TableModel result = gson.fromJson(new String(responseBody), TableModel.class);
                        //1表示红点，0表示灰点  //但是真实的数据时0红点 1灰点
                        if (1 == Tools.isIntNull(result.getSuccess())) {
                            if (!result.getLesson_date().equals("") && result.getLesson_date() != null) {
                                tempdata = result.getLesson_date();
                                Intent intent = new Intent();
                                intent.setClass(context, TimeTableActivity.class);
                                intent.putExtra("data", tempdata);
                                intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                                context.startActivity(intent);
                            }
                        }

                    } catch (Exception e) {

                    }
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

            }

            @Override
            public void onFinish() {
                super.onFinish();
            }
        });
    }

}
